# -*- coding: utf-8 -*-
# Project : flacon
# File : wv_encoder.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


import encoder
import errors
from settings import *
from flaconglobal import *
from PyQt4 import QtCore
import sys, re
import wv_config


class WVEncoder(encoder.Encoder):
    id = 'WV'
    extension = 'wv'
    name = 'WavPack'


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def trackGainClass(cls):
        return WvGain


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def configPageClass(cls):
        return wv_config.WvConfig


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def programs(cls):
        return ['wavpack']


   #****************************************************************
   #
   #****************************************************************
    def getEncoderArgs(self):
        args = QStringList()
        args << settings.value("Programs/wavpack").toString()
        args << "-q"            # Suppress progress indicator

        # Settings .................................................
        (compression, ok) = settings.value("WV/Compression").toInt()
        if compression == 0:    args << "-f"
        if compression == 1:    args << "-h"
        if compression == 2:    args << "-hh"

        # Tags .....................................................
        if (self._track.artist()):  args << "-w" << QString("Artist=%1").arg(self._track.artist())
        if (self._track.album()):   args << "-w" << QString("Album=%1").arg(self._track.album())
        if (self._track.genre()):   args << "-w" << QString("Genre=%1").arg(self._track.genre())
        if (self._track.year()):    args << "-w" << QString("Year=%1").arg(self._track.year())
        if (self._track.title()):   args << "-w" << QString("Title=%1").arg(self._track.title())
        if (self._track.discId()):  args << "-w" << QString("DiscId=%1").arg(self._track.discId())
        if (self._track.comment()): args << "-w" << QString("Comment=%1").arg(self._track.comment())
        args << "-w" << QString("Track=%1/%2").arg(self._track.trackNum()).arg(self._track.trackSet().count())

        args << "-"
        args << "-o" << self._outFile,

        return args



class WvGain(encoder.Gain):
   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def programs(cls):
        return ['wvgain']


   #****************************************************************
   #
   #****************************************************************
    def getArgs(self):
        args = QStringList()
        args << settings.value("Programs/wvgain").toString()
        args << "-a"

        args << self._files

        return args



#****************************************************************
# Register encoder & gain classes
#****************************************************************
encoder.registerEncoder(WVEncoder)
